import wx
import sys
from threading import Thread
from app_frame import WordLearnerFrame
from data_loader import WordDataLoader
from config import ConfigManager


class WordLearnerApp(wx.App):
    def OnInit(self):
        # 初始化配置
        self.config = ConfigManager()

        # 初始化数据加载器
        try:
            self.data_loader = WordDataLoader("words.xlsx")
        except Exception as e:
            wx.MessageBox(f"无法加载单词数据: {str(e)}", "错误", wx.OK | wx.ICON_ERROR)
            return False

        # 创建并显示主窗口
        self.frame = WordLearnerFrame(self.config, self.data_loader)
        self.frame.Show()
        return True


def run_gui():
    app = WordLearnerApp(False)
    app.MainLoop()


if __name__ == "__main__":
    # 在单独的线程中运行GUI
    gui_thread = Thread(target=run_gui)
    gui_thread.daemon = True
    gui_thread.start()

    # 主线程可以继续执行其他任务
    try:
        while gui_thread.is_alive():
            gui_thread.join(0.1)
    except KeyboardInterrupt:
        sys.exit(0)